﻿using _11._24._2014_AngularPractice.Data;
using _11._24._2014_AngularPractice.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace _11._24._2014_AngularPractice.Adapters.Data
{
    public class CarDataAdapter : ICarAdapter
    {
        public FavoriteCarsViewModel GetCarViewModels(string userId)
        {
            FavoriteCarsViewModel model;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Users.Where(u => u.Id == userId).Select(u => new FavoriteCarsViewModel
                {
                    FirstName = u.FirstName,
                    LastName = u.LastName,
                    Cars = u.UserCars.Select(uc => new CarViewModel
                    {
                        Make = uc.Car.Make,
                        Model = uc.Car.Model,
                        Year = uc.Car.Year
                    }).ToList()
                }).FirstOrDefault();
            }

            return model;
        }
    }
}